//
//  ZAQTabBar.swift
//  ZAQWBDZT
//
//  Created by 赵安庆 on 16/9/19.
//  Copyright © 2016年 zaq. All rights reserved.
//

import UIKit
// 定义代理
protocol ZAQTarBarDelegate:NSObjectProtocol {
    func didSelectedComposeButton ()
}
class ZAQTabBar: UITabBar {
//点击按钮执行闭包
    var composeButtonClosure:(()->())?
// 定义代理对象
    weak var zaqDelegate: ZAQTarBarDelegate?

    private lazy var composeButton: UIButton =  {
        let button = UIButton()

        button.addTarget(self, action: #selector(composeButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)

    button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .selected)

        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)

        //  设置大小
        button.sizeToFit()
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
         setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
         setupUI()
    }
    private func setupUI(){

        addSubview(composeButton)
    }

    //设置子控件的 位置
    override func layoutSubviews() {
//   别忘了掉super的
        super.layoutSubviews()
        composeButton.centerX = width / 2
        composeButton.centerY = height / 2

        let  itemWith  =  width / 5

        var index = 0

        for value in subviews{
            if value.isKind(of: NSClassFromString("UITabBarButton")!) {
                value.width = itemWith
                value.x = CGFloat(index) * itemWith
                
                index += 1
                if index == 2 {
                    index += 1
                }
            }
        }


    }

    func composeButtonAction(){
//        print("111")

        composeButtonClosure?()
//点击按钮的时候使用代理方法
        zaqDelegate?.didSelectedComposeButton()
    }

}
